package com.tsingteng.cosfun.crashUtils.utils;

import android.text.TextUtils;

import com.qiniu.pili.droid.shortvideo.PLShortVideoUploader;
import com.qiniu.pili.droid.shortvideo.PLUploadResultListener;
import com.qiniu.pili.droid.shortvideo.PLUploadSetting;
import com.tsingteng.cosfun.app.AppContext;
import com.tsingteng.cosfun.bean.BaseDataBean;
import com.tsingteng.cosfun.bean.VideoSignBean;
import com.tsingteng.cosfun.http.RxRetrofit;
import com.tsingteng.cosfun.http.RxSchedulers;
import com.tsingteng.cosfun.utils.AppUtils;
import com.tsingteng.cosfun.utils.LogUtils;

import org.json.JSONException;
import org.json.JSONObject;

import io.reactivex.observers.DisposableObserver;

/**
 * 描述 :
 * 创建人: 肖利.
 * 时间:2018/11/2
 * qq(872179966)
 */

public class ExceptionUpLoader {
    private static ExceptionUpLoader mInstance = null;

    private ExceptionUpLoader() {
    }

    public static ExceptionUpLoader getmInstance() {
        if (mInstance == null) {
            synchronized (ExceptionUpLoader.class) {
                if (mInstance == null) {
                    mInstance = new ExceptionUpLoader();
                }
            }

        }
        return mInstance;
    }


    public void upLoadCrashLog(ExceptionUpLoadListener exceptionUpLoadListener) {
        final int[] typeUpload = {0};
        LogUtils.e("upLoadException","请求七牛token中!");
        RxRetrofit.getApiSever().getSignaTure("9").compose(RxSchedulers.io_main()).subscribe(new DisposableObserver<BaseDataBean<VideoSignBean>>() {
            @Override
            public void onNext(BaseDataBean<VideoSignBean> videoSignBeanBaseDataBean) {
                String haveException = AppUtils.isHaveException();
                //不是200请求失败,直接返回
                if (videoSignBeanBaseDataBean.getStatus()!=200) {
                    LogUtils.e("upLoadException","fail to get qiniu token!");
                    return;
                }
                if (TextUtils.isEmpty(haveException)) {
                    LogUtils.i("upLoadException","has no exceptions!");
                    return;
                }
                String[] split = haveException.split(",");
                if (split.length <= 0) {
                    LogUtils.e("upLoadException","malformat exception " + haveException);
                    return;
                }
                LogUtils.i("upLoadException","00000"+ split[0]+ "111111" + split[1]);
                PLShortVideoUploader mVideoUploadManager = new PLShortVideoUploader(AppContext.getContext(), new PLUploadSetting());
                typeUpload[0] = 0;
                mVideoUploadManager.startUpload(split[0], split[0],videoSignBeanBaseDataBean.getData().getSign());

                LogUtils.v("upLoadException","上传七牛中!");
                mVideoUploadManager.setUploadResultListener(new PLUploadResultListener() {
                    @Override
                    public void onUploadVideoSuccess(JSONObject response) {
                        try {
                            AppUtils.setHaveException(null);
                            final String filePath = videoSignBeanBaseDataBean.getData().getDomain() + "/" + response.getString("key");
                            LogUtils.i("upLoadException","upload succeed " + filePath);
                            if ( typeUpload[0] == 0) {
                                RxRetrofit.getApiSever().getExceptionUpLoadData(filePath).compose(RxSchedulers.io_main()).subscribe();
                                mVideoUploadManager.startUpload(split[1], split[1] ,videoSignBeanBaseDataBean.getData().getSign());
                                typeUpload[0]=1;
                            }else {
                                RxRetrofit.getApiSever().getExceptionUpLoadData(filePath).compose(RxSchedulers.io_main()).subscribe();
                                exceptionUpLoadListener.exceptionSuccess();
                            }

                        } catch (JSONException e) {
                            e.printStackTrace();
                        }
                    }

                    @Override
                    public void onUploadVideoFailed(int i, String s) {
                        exceptionUpLoadListener.exceptionFail();
                        LogUtils.e("upLoadException","七牛上传失败!");
                    }
                });
            }

            @Override
            public void onError(Throwable e) {
                LogUtils.e("upLoadException","请求七牛token失败!");
            }

            @Override
            public void onComplete() {
                LogUtils.e("upLoadException","请求七牛token完成!");
            }
        });
    }

    public interface ExceptionUpLoadListener{
        void exceptionSuccess();
        void exceptionFail();
    }
}
